<?php

namespace app\store\controller;

use think\Controller;
use app\common\controller\store;
use app\common\library\Email;


//客户控制器
class Address extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->BusinessModel = model('Business.Business');
        $this->AddressModel = model('Business.Address');

        //这里下面的所有方法都需要用到先查询是否存在这个用户，所以把这个公共部分提取出来

         //接收在vue-shop\src\components\address\index.vue传过来的用户的id。为什么在构造函数接收传过来的id，因为下面的增删查改方法，都是先要查到用户，才能往下操作，所以现在构造函数先把用户查询出来
        $busid = $this->request->param('busid','0','trim');

        // 查询
        $this->business = $this->BusinessModel->where(['id'=>$busid])->find();

        if(!$this->business)
        {
            $this->error('用户不存在');
            die;
        }
    }

    // 收货地址查询列表
    public function index()
    {
        if($this->request->isPost())
        {
            // 找出来这个用户的地址
            $address = $this->AddressModel->where(['busid'=>$this->business['id']])->select();

            if($address)
            {
                $this->success('查询收货地址',null,$address);
                die;
            }else
            {
                $this->error('暂无收货地址');
                die;
            }

        }

    }

    // 添加收货地址
    public function add()
    {
        if($this->request->isPost())
        {
            // 接收数据
            // $busid= $this->request->param('busid',null,'trim');
            // $consignee = $this->request->param('consignee','','trim');
            // $address= $this->request->param('addressDetail','','trim');
            // $code = $this->request->param('code','','trim');
            // $mobile= $this->request->param('mobile','','trim');
            // $status = $this->request->param('status', '0', 'trim');

            $params = $this->request->param();

            $status = $this->request->param('status', '0', 'trim');
            $status = $this->request->param('status', '0', 'trim');

            // 判断是否有地区码
            if(!empty($code))
            {
                //查询是否有地区数据 
                $parent = model('Region')->where(['code'=>$code])->value('parentPath');
                   
                if(!empty($parent))
                {
                    // 把字符串变成数组
                    $arr = explode(',',$parent);

                    $params['province'] = isset($arr[0]) ? $arr[0] : null;
                    $params['city'] = isset($arr[1]) ? $arr[1] : null;
                    $params['district'] = isset($arr[2]) ? $arr[2] : null;
                }
            }

            // 开启事务
            $this->AddressModel->startTrans();

            // 判断是否选择了默认收货地址
            if($status == '1')
            {
                // 直接去更新覆盖，将已有的数据变为0。因为当status==1时，这个地址就是默认的收货地址
                $AddressStatus = $this->AddressModel->where(['busid'=>$this->business['id']])->update(['status' => '0']);

                if($AddressStatus === FALSE)
                {
                    $this->error('更新默认地址状态有误');
                    die;
                }
            }

            // 插入数据
            $result = $this->AddressModel->validate('common/Business/Address')->save($params);

            if($result === FALSE)
            {
                $this->AddressModel->rollback();
                $this->error($this->AddressModel->getError());
            }else
            {
                $this->AddressModel->commit();
                $this->success('添加成功');
                die;
            }
        }
    }

    // 查询收货地址
    public function info()
    {
        if($this->request->isPOST())
        {
            // 接收数据。不用另外在接收busid,直接在$this->business['id']可以拿到
            $id = $this->request->param('id',0,'trim');

            // 组装查找的条件
            $where = [
                'id' => $id,
                'busid' => $this->business['id']
            ];

            $address = $this->AddressModel->where($where)->find();
           
            // var_dump($address->toArray());
            // exit;

            if($address)
            {
                $this->success('返回收货地址',null,$address);
                die;

            }else
            {
                $this->error('没有这个地址');
                die;
            }
        }
    }
    
    // 编辑收货地址
    public function edit()
    {
        if($this->request->isPOST())
        {
            $params = $this->request->param();

            $id = $this->request->param('id',0,'trim');
            $code = $this->request->param('code','','trim');
            $status = $this->request->param('status','0','trim');

            // var_dump($data);
            // die;

            $where = [
                'id' => $id,
                'busid' => $this->business['id']
            ];

            // 查找数据
            $address = $this->AddressModel->where($where)->find();

            // var_dump($address->toArray());
            // die;

            if(!$address)
            {
                $this->error('收货地址不存在');
                die;
            }

            // 判断是否有地区码
            if(!empty($code))
            {
                // 查询省市区的地区码出来
                $parent = model('Region')->where(['code' => $code])->
                value('parentpath');

                if(!empty($parent))
                {
                    $arr = explode(',',$parent);

                    $params['province'] = isset($arr[0]) ? $arr[0] : null;
                    $params['city'] = isset($arr[1]) ? $arr[1] : null;
                    $params['district'] = isset($arr[2]) ? $arr[2] : null;
                }
            }

                   
            // 开启事务
            $this->AddressModel->startTrans();

            // 判断是否选择了默认收货地址
            if($status == '1')
            {
                // 直接去更新覆盖，将已有的数据变为0
                $AddressStatus = $this->AddressModel->where(['busid' => $this->business['id']])->update(['status' => '0']);

                if($AddressStatus === FALSE)
                {
                    $this->error('更新默认状态有误');
                    die;
                }
            }

            // 编辑数据
            $result = $this->AddressModel->validate('common/Business/Address')->isUpdate(true)->save($params);


                    // var_dump($result);
                    // die;


            if($result === FALSE)
            {
                $this->AddressModel->rollback();
                $this->error($this->AddressModel->getError());
                die;
            }else
            {
                $this->AddressModel->commit();
                $this->success('更新成功');
                die;
            }
        }
    }

    // 删除
    public function del()
    {
        if($this->request->isPOST())
        {

            $params = $this->request->param();
            $id = $this->request->param('id', 0 ,'trim');

            $where = [
                'id' => $id,
                'busid' => $this->business['id']
            ];

            $address = $this->AddressModel->where($where)->find();

            if(!$address)
            {
                $this->error('收货地址不存在');
                die;
            }

            $result = $this->AddressModel->destroy($id);

            if($result === FALSE)
            {
                $this->error('删除失败');
                die;
            }else
            {
                $this->success('删除成功');
                die;
            }
        }
    }

    // 设置为默认收货地址
    public function check()
    {
        if($this->request->isPOST())
        {
           $id = $this->request->param('id','0','trim');

            $where = [
                'id' => $id,
                // 'busid' => $this->business['id']
                'busid' =>$this->business->id
            ];

            $address = $this->AddressModel->where($where)->find();

            if(!$address)
            {
                $this->error('收货地址不存在');
                die;
            }

            if($address['status'] == '1')
            {
                $this->error('已经是默认地址');

                return false;
                // die;
            }

            // 先去更新默认地址
            $status = $this->AddressModel->where(['busid'=>$this->business['id']])->update(['status' => '0']);

            if($status === FALSE)
            {
                $this->error('取消默认地址失败');
                exit;
            }

            //默认指定的默认地址
            $data = [
                'id' => $id,
                'status' => '1'
            ];

            $result = $this->AddressModel->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->AddressModel->getError());
                die;
            }else
            {
                $this->success('更新默认地址成功');
                die;
            }


        }

    }

    







}
